extends HBoxContainer
class_name CardContainer


@onready var bridge_image: TextureRect = $CardImage
@onready var label: Label = $Label

var cooldown_time := 0.0
var cur_cooldown_duration := 0.0
var timer:Timer
var tween:Tween
var current_bridge: BaseBridge
var origin_position

func _ready() -> void:
	timer = Timer.new()
	timer.wait_time = 0.1
	timer.timeout.connect(on_time_out)
	add_child(timer)
	origin_position = position

func set_bridge(bridge:BaseBridge) -> void:
	current_bridge = bridge
	bridge_image.texture = bridge.get_texture()
	label.text = bridge.birdge_name

func _process(delta: float) -> void:
	if Engine.get_process_frames() % 60 == 0:
		if modulate.a < .8:
			label.visible = false
		else:
			label.visible = true
	
	

func shake() -> void:
	if tween != null && tween.is_running():
		tween.kill()
	tween = create_tween()
	tween.set_parallel()
	var randf_offset = Mathf.create_randf_offset(20)
	tween.tween_property(bridge_image,"position",origin_position +\
	 Vector2(0,3).rotated(randf_offset), 0.1)
	tween.tween_property(bridge_image,"scale", Vector2(1.2,1.2),0.1)
	tween.tween_property(bridge_image,"rotation", randf_offset,0.1)
	tween.chain()
	
	tween.tween_property(bridge_image,"position",origin_position, 0.1)
	tween.tween_property(bridge_image,"scale", Vector2.ONE,0.1)
	tween.tween_property(bridge_image,"rotation", 0,0.1)



func set_cooldown(duration: float) -> void:
	cur_cooldown_duration = duration
	timer.start()
	

func on_time_out() -> void:
	if cur_cooldown_duration > 0:
		cur_cooldown_duration -= 0.1
	else:
		timer.stop()

func reset() -> void:
	bridge_image.texture = null
